<template>
	<view class="container">
		<view class="set-box">
			<view class="title">保存文件</view>
			<textarea placeholder="请输入文本内容" v-model="content"></textarea>
			<view class="control">
				<input type="text" placeholder="请输入文本名" v-model="fileName" />
				<view class="btn" @click="dowload">下载文件</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				content: '',
				fileName: ''
			};
		},
		methods: {
			dowload() {
				if (!this.content) {
					uni.showToast({
						title: '请输入内容',
						icon: 'none'
					})
					return
				}
				if (!this.fileName) {
					uni.showToast({
						title: '请输入文件名',
						icon: 'none'
					})
					return
				}
				let ele = document.createElement('a');
				let blob = new Blob([this.content], {
					type: 'text/plain'
				});
				let url = URL.createObjectURL(blob);
				ele.href = url;
				ele.download = this.fileName;
				ele.click();
			}
		}
	}
</script>

<style lang="scss">
	.container {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		height: 100vh;
		background: linear-gradient(to right bottom, lightblue 40%, orange);
	}

	.set-box {
		width: 1200rpx;
		height: 700rpx;
		padding: 32rpx;
		border-radius: 32rpx;
		background-color: white;

		.title {
			text-align: center;
			font-size: 48rpx;
			color: slateblue;
		}

		textarea {
			width: 100%;
			height: 70%;
			margin-top: 32rpx;
			padding: 32rpx;
			box-sizing: border-box;
			border: 2rpx solid mediumvioletred;
		}

		.control {
			display: flex;
			justify-content: space-around;
			align-items: center;
			margin-top: 32rpx;

			input,
			.btn {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 300rpx;
				height: 80rpx;
				padding: 0 32rpx;
				background-color: deeppink;
				color: lightpink;
				box-sizing: border-box;
			}

			input {
				background-color: white;
				border: 2rpx solid deeppink;
				color: black;
			}
		}
	}
</style>